package org.ekstrabilet.web.beans;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.ekstrabilet.logic.exceptions.StadiumException;
import org.ekstrabilet.stadium.beans.Row;
import org.ekstrabilet.stadium.beans.Seat;
import org.ekstrabilet.stadium.beans.Sector;
import org.ekstrabilet.stadium.beans.Stadium;
import org.ekstrabilet.stadium.beans.Tribune;
import org.ekstrabilet.stadium.constants.StadiumConstants;
import org.ekstrabilet.stadium.logic.StadiumLogic;

public class StadiumBean {
	
	private String name;
	private String city;
	private int capacity;
	private int tribune0;
	private int tribune1;
	private int tribune2;
	private int tribune3;
		
	
	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getCity() {
		return city;
	}


	public void setCity(String city) {
		this.city = city;
	}


	public int getCapacity() {
		return capacity;
	}


	public void setCapacity(int capacity) {
		this.capacity = capacity;
	}


	public int getTribune0() {
		return tribune0;
	}


	public void setTribune0(int tribune0) {
		this.tribune0 = tribune0;
	}


	public int getTribune1() {
		return tribune1;
	}


	public void setTribune1(int tribune1) {
		this.tribune1 = tribune1;
	}


	public int getTribune2() {
		return tribune2;
	}


	public void setTribune2(int tribune2) {
		this.tribune2 = tribune2;
	}


	public int getTribune3() {
		return tribune3;
	}


	public void setTribune3(int tribune3) {
		this.tribune3 = tribune3;
	}


	public String save() {
		Stadium stadium = new Stadium();
		int[] sectors = new int[4];
		sectors[0] = tribune0;
		sectors[1] = tribune1;
		sectors[2] = tribune2;
		sectors[3] = tribune3;
		
		stadium.setCapacity(capacity);
		stadium.setName(name);
		stadium.setCity(city);
		int row1 = 0;
		int row2 = 0;
		//tribunes
		Tribune[] tribunes = new Tribune[4];
		int sign = 65; //ascii a
		for(int i = 0 ; i < 4 ; i++) {
			tribunes[i] = new Tribune();
			tribunes[i].setCapacity(capacity/4);
			tribunes[i].setNumber(i);
			//tribune 0 & 1
			if(i<2){
				int m1 = tribunes[i].getCapacity();
				int x1 = StadiumConstants.pW;
				row1 = m1 / (x1 / StadiumConstants.seatS) + 1;
			}
			//tribune 2 & 3
			else {
				int m2 = tribunes[i].getCapacity();
				int y2 = StadiumConstants.pH + 2 * StadiumConstants.seatS * row1;
				row2 = m2 / (y2 / StadiumConstants.seatS) + 1; 
			}
			//sectors
			for(int j = 0 ; j < sectors[i] ; j++) {
				Sector s = new Sector();
				s.setSign((char) sign++);
				s.setCapacity(tribunes[i].getCapacity() / sectors[i]);
				int r = 0;
				if(i < 2) r = row1;
				else r = row2;
				for(int k = 0 ; k < r ; k++) {
					Row row = new Row();
					row.setCapacity(s.getCapacity()/r);
					for(int l = 0; l < row.getCapacity() ; l++ ) {
						Seat seat = new Seat();
						seat.setEmpty(true);
						row.addSeat(seat);
					}
					s.addRow(row);
				}
				
				tribunes[i].addSector(s);
				
			}
		}
		stadium.setTribunes(tribunes);
		
		try {
			StadiumLogic.getInstance().addStadium(stadium);
			FacesContext.getCurrentInstance().addMessage("stadiumForm:save", new FacesMessage("Succesful!"));
		} catch (StadiumException e) {
			FacesContext.getCurrentInstance().addMessage("stadiumForm:save", new FacesMessage(e.getMessage()));
		}
		return null;
	}
	
	
	
	
}
